/*----------------FireFox Extension JavascriptFile-------------------*/
var saveVideoExtensionVersion=1.0;

var timer;
var interval=1;
var appendedClassName="save-video-appended";
var downloadURL = "http://www.save-video.com/download.php?s=ffext&url=";

var downloadBtnPath = "http://www.save-video.com/img/extension/";




$(document).ready(function(){


var timer= setTimeout(checkPageContent,interval*1000);
//setExtensionVersion();

});

function checkPageContent(){

	checkYoutubeSearchResults();
	checkYoutubeWatchPage();

	setTimeout(checkPageContent,interval*1000);

}

function checkYoutubeSearchResults(){

	var searchResults = $(".yt-lockup-video:not(."+appendedClassName+")");
	if(searchResults.length>0){
		
		
		 searchResults.find(".yt-uix-tile-link").each(function(){
		 
		 var videoURL=$(this).attr("href");
		var videoURL = "http://www.youtube.com"+videoURL;
		var img = createYoutubeDownloadButton(videoURL,"small.png");
		 
		 /*
			    img = new Image();
				img.src=chrome.extension.getURL("downloadicon.png");
				img.style.width="20px";
				img.style.height="20px";
				img.style.marginLeft="4px";
				img.style.marginRight="4px";
				img.title="Download this video";
				
				
				$(img).on("click",function(e){
					document.location=downloadURL+videoURL;
					e.preventDefault();
				});
		*/
				$(this).prepend(img);

				$(this).parents(".yt-lockup-video").first().addClass(appendedClassName);
		});
	}


}

function checkYoutubeWatchPage(){

	var watchTitle=$(".watch-title:not(."+appendedClassName+")")
	if(watchTitle.length>0){
		
		 //img = new Image();
		 var videoURL = document.location.href;
		 var img=createYoutubeDownloadButton(videoURL,"medium.png");
		 console.log(img);
		 /*
			img.src=chrome.extension.getURL("downloadicon.png");
			img.style.width="20px";
			img.style.height="20px";
			img.style.marginLeft="4px";
			img.style.marginRight="4px";
			img.style.cursor="pointer";
			img.title="Download this video";
			
			//var videoURL=$("link[rel=canonical]").attr("href");
			var videoURL = document.location.href;
			$(img).on("click",function(e){
				document.location=downloadURL+videoURL;
				e.preventDefault();
			});
			*/
		watchTitle.prepend(img);
		watchTitle.addClass(appendedClassName);
	
	}

}
function createYoutubeDownloadButton(videoURL,imgName){

			var imgPath=downloadBtnPath+"yt/"+imgName;
			
			var img = '<a href="'+downloadURL+videoURL+'"><img src="'+imgPath+'" style="margin-left:4px;margin-right:4px; border-width:0px;vertical-align:middle;" title="Download This Video" /></a>';
			return img;

}